﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LNBMS.BLL;
using LNBMS.Entity;
using Newtonsoft.Json;
using DotNet.Utilities;
using log4net;

namespace LNBMS.Web.Interface
{
    public partial class CheckUsername : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            string cardId = Request.QueryString["cardid"];
            Response.Write(Check(cardId));
        }

        #region  匹配身份
        /// <summary>
        /// 匹配身份
        /// </summary>
        /// <param name="cardId"></param>
        /// <returns></returns>
        private static string Check(string cardId)
        {
            InformationInputBLL informationinputbll = new InformationInputBLL();
            InformationInput informationInput = new InformationInput();
            Msg msg = new Msg();
            if (informationinputbll.GetEntity(cardId) != null)
            {
                informationInput = informationinputbll.GetEntity(cardId);
                var resultContent = new
                {
                    CardId = informationInput.cardid,
                    Name = informationInput.name,
                    Zone = ZoneidToName(informationInput.zoneId)
                };
                msg.ReturnContent = resultContent;
                msg.Status = "获取信息成功！";
            }
            else
            {
                msg.Status = "该林农信息不存在！";
            }
            msg.RequestId = cardId;
            return JsonConvert.SerializeObject(msg, Formatting.Indented);
        }

        public static string ZoneidToName(string zoneId)
        {
            string[] queryParams = new string[4];
            int zoneIdLength = zoneId.Length;
            queryParams[0] = zoneId.Substring(0, 4);
            var cityInfo = new CityInfo();
            var cityInfoBLL = new CityInfoBLL();
            var countyInfo = new CountyInfo();
            var countyInfoBLL = new CountyInfoBLL();
            var townInfo = new TownInfo();
            var townInfoBLL = new TownInfoBLL();
            var villageInfo = new VillageInfo();
            var villageInfoBLL = new VillageInfoBLL();
            cityInfo = cityInfoBLL.GetEntity(queryParams[0]);
            if (zoneIdLength > 4)
            {
                queryParams[1] = zoneId.Substring(0, 6);
                countyInfo = countyInfoBLL.GetEntity(queryParams[1]);
            }
            if (zoneIdLength > 6)
            {
                queryParams[2] = zoneId.Substring(6, 3);
                townInfo = townInfoBLL.GetEntity(queryParams[1], queryParams[2]);
            }
            if (zoneIdLength > 9)
            {
                queryParams[3] = zoneId.Substring(9, 3);
                villageInfo = villageInfoBLL.GetEntity(queryParams[1], queryParams[2], queryParams[3]);
            }
            return cityInfo.name + countyInfo.name + townInfo.name + villageInfo.name;
        }
        #endregion
    }
}